package
{
	import com.riaidea.utils.zip.ZipArchive;
	import com.riaidea.utils.zip.ZipEvent;
	import com.riaidea.utils.zip.ZipFile;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.utils.Dictionary;
	
	import model.UnitKey;
	import model.user.User;
	
	import ricky.loader.MultipleLoader;
	import ricky.loader.Request;
	import ricky.plutonium.Application;
	
	import ui.loading.Loading;
	
	[SWF(frameRate = 60, width = 1024, height = 700, backgroundColor = 0x000000)]
	public class Loader extends Sprite
	{
		public function Loader()
		{
			super();
			
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;	
			stage.frameRate=60;
			
			
			loading=new Loading();
			loading.x=(this.stage.stageWidth-800)/2
			loading.y=this.stage.stageHeight-200;
			this.addChild(loading);
			var url:String;
			var obj:Object=loaderInfo.parameters;
			for(var v:String in obj)
			{
				url=v;
			}
			var d:Date=new Date();
			var usfri:Object={"userName":"pu"+d.hours+"."+d.minutes};
			
			conf=new Object();
			Application.parameters[ApplicationParametersAttributes.userInfo]=usfri;
			var req5:Request=new Request("NetGame.swf?"+url,onMainComplete,1);//main 
			var req1:Request=new Request("config\\language\\lan.zip?"+url,onLanComplete,4);
			var req2:Request=new Request("config\\ability\\ability.zip?"+url,onAbiliComplete,3);
			var req3:Request=new Request("config\\unit\\unit.zip?"+url,onUnitComplete,2);
			MultipleLoader.instance.loadReq([req1,req2,req3,req5],onComplete,loading.onProgress);
		}
		
		private function onMainComplete(e:Request):void
		{
			trace("game 4");
			main=e.data;
		}
		
		private function onComplete(e:*):void
		{
			MultipleLoader.instance.allCompelte=null;
			MultipleLoader.instance.onProgress=null;
			
			if(main)
			{
				loading.parent.removeChild(loading);
//				main["loading"]=loading;
				main["loadConfig"](conf);
				this.stage.addChild(main);
			}
		}
		
		private function onUnitComplete(e:Request):void
		{
			trace("unit 3");
			
			var zip:ZipArchive=new ZipArchive();
			zip.open(e.data);

			var unit:Object=new Object();
			conf["unit"]=unit;
			for each(var key:String in UnitKey.units)
			{
				var f:ZipFile=zip.getFileByName(key);
				unit[key]=f.data.toString();
			}
			
		}
		
		private function onAbiliComplete(e:Request):void
		{
			trace("Ability 2");
			var ability:Object=new Object();
			conf["ability"]=ability;
			var zip:ZipArchive=new ZipArchive();
			zip.open(e.data);
			
			//初始化技能数据
			var key1:String=UnitKey.KeyAbilityData;
			var f:ZipFile=zip.getFileByName(key1);
			ability[key1]=f.data.toString();
			
			//初始化技能
			var key2:String=UnitKey.KeyAbility;
			f=zip.getFileByName(key2);
			ability[key2]=f.data.toString();
			
		}
		
		
		private function onLanComplete(e:Request):void
		{
			trace("Lan 1");
			var zip:ZipArchive=new ZipArchive();
			zip.open(e.data);
			var f:ZipFile=zip.getFileByName("lan");
			CLocale.loadLanguage(f.data.toString());
			
			var host:String=CLocale.getValue("host");
			
			Application.parameters[ApplicationParametersAttributes.HOST]=host;
			Application.parameters[ApplicationParametersAttributes.PORT]="9001";
		}
		
		//
		private var loading:Loading
		public var main:Sprite;
		private var conf:Object;
	}
}